package cn.openea.perpartner.meily.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.GroupBuying;
import cn.openea.perpartner.meily.web.entity.dto.GroupBuyingDTOFactory;
import cn.openea.perpartner.meily.web.entity.dto.GroupDto;
import cn.openea.perpartner.meily.web.service.IGroupBuyingService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (GroupBuying)表控制层
 *
 * @author rains
 * @since 2023-02-07 10:31
 */
@Api(tags = "团购")
@RestController
@RequestMapping("/buying")
public class GroupBuyingController extends BaseController {

    @Resource
    private IGroupBuyingService groupBuyingService;
    @Resource
    private GroupBuyingDTOFactory groupBuyingDTOFactory;


    @ApiOperation("发布团购")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody GroupBuying groupBuying) throws ParseException {
        return this.groupBuyingService.publish(groupBuying);
    }

    @ApiOperation("结束团购")
    @PostMapping("/finish")
    public AjaxResult finish(@RequestBody GroupBuying groupBuying) {
        return this.groupBuyingService.finish(groupBuying);
    }

    @ApiOperation("获取团购详情")
    @PostMapping("/queryById")
    public AjaxResult queryById(@RequestBody JSONObject object) {
        if (object.isEmpty()) {
            return AjaxResult.error("获取不到参数！");
        }
        return this.groupBuyingService.queryById(object.getString("id"));
    }

    @ApiOperation("修改团购数据")
    @PostMapping("/update/group/buying")
    public AjaxResult updateGroupBuying(@RequestBody GroupBuying groupBuying){
        groupBuyingService.addPurchaseRecord(groupBuying.getId(),
                Integer.parseInt(groupBuying.getCustomerNum()),
                Integer.parseInt(groupBuying.getUsedQuantity()));
        return AjaxResult.success();
    }

    @ApiOperation("获取正在进行中的拼团列表")
    @PostMapping("/getSimilarGroups")
    public AjaxResult getSimilarGroups(@RequestBody GroupDto groupDto) {
        return this.groupBuyingService.getSimilarGroups(groupDto);
    }

    @ApiOperation("获取我发布的团购列表")
    @PostMapping("/getPublishGroupPurchaseList")
    public TableDataInfo getPublishGroupPurchaseList1(@RequestBody GroupDto groupDto) {
        startPage();
        List<GroupBuying> list = groupBuyingService.getPublishGroupPurchaseList(groupDto);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setRows(list.stream()
                .map((item) -> groupBuyingDTOFactory.groupBuyingToInfoDto.apply(item))
                .collect(Collectors.toList()));
        return dataTable;
    }

    @ApiOperation("获取我发布的团购列表")
    @GetMapping("/publish/group/purchase/list")
    public TableDataInfo getPublishGroupPurchaseList(GroupDto groupDto) {
        startPage();
        List<GroupBuying> list = groupBuyingService.getPublishGroupPurchaseList(groupDto);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setRows(list.stream()
                .map((item) -> groupBuyingDTOFactory.groupBuyingToInfoDto.apply(item))
                .collect(Collectors.toList()));
        return dataTable;
    }

    @ApiOperation("获取我发布的团购数据，如代理销售商品数据，实际收入数据")
    @GetMapping("/publish/group/purchase/data/{initiatorId}")
    public AjaxResult getPublishGroupPurchaseData(@PathVariable String initiatorId){
        GroupBuyingDTOFactory.GroupBuyingStatisticsDto groupBuyingStatisticsDto = groupBuyingService
                .getPublishGroupDataByInItiatorId(initiatorId);
        return AjaxResult.success(groupBuyingStatisticsDto);
    }

    @ApiOperation("获取我发布的团购总数")
    @PostMapping("/getPublishGroupPurchaseNum")
    public AjaxResult getPublishGroupPurchaseNum(@RequestBody GroupDto groupDto) {
        if(StrUtil.isBlank(groupDto.getInitiatorId())){
            return AjaxResult.error("获取不到当前用户的userid，请检查是否登录！");
        }
        return AjaxResult.success("获取我发布的团购总数成功！", this.groupBuyingService.getPublishGroupPurchaseNum(groupDto));
    }

    @ApiOperation("获取我参与的团购列表-暂时无用")
    @PostMapping("/getJoinGroupPurchaseList")
    public TableDataInfo getJoinGroupPurchaseList(@RequestBody GroupDto groupDto) {
        startPage();
        List<GroupBuying> list = this.groupBuyingService.getJoinGroupPurchaseList(groupDto);
        return getDataTable(list);
    }

    @ApiOperation("获取我参与的团购总数-暂时无用")
    @PostMapping("/getJoinGroupPurchaseNum")
    public AjaxResult getJoinGroupPurchaseNum(@RequestBody GroupDto groupDto) {
        return this.groupBuyingService.getJoinGroupPurchaseNum(groupDto);
    }



}
